import { AnyTxtRecord } from "dns";

namespace jsjinjieday4 {

    interface obj1 {
        [key: string]: string | number | any;
    }

    // 递归实现深copy
    function deepCopy(obj: obj1): obj1 {
        if (typeof obj !== 'object' || obj === null) {
            return obj;
        }
        let result: any = Array.isArray(obj) ? [] : {};
        for (let key in obj) {
            if (obj.hasOwnProperty(key)) {
                result[key] = deepCopy((obj as any)[key]);
            }
        }
        return result;
    }

    let obj2: obj1 = {
        a: 1,
        b: [1, 2, 3]
    }
    let obj3 = deepCopy(obj2);
    obj3.b[0] = 99;
    console.log(obj3.b);
    console.log(obj2.b);


    // 防抖函数
    function debonuce(func: Function, delay: number): Function {
        let timer: NodeJS.Timeout | null = null;
        return function (this: any) {
            let constext: any = this;
            let args: IArguments = arguments;
            timer ? clearTimeout(timer) : null;
            timer = setTimeout(() => {
                func.apply(constext, args);
            }, delay)
        }
    }
    function s1() {
        console.log('s1');

    }

    debonuce(s1, 1000)()

    // 节流函数

    function throttle(func: Function, limit: number): Function {
        let inThrottle: any;
        return function (this: any) {
            const context: any = this;
            const args: IArguments = arguments;
            if (!inThrottle) {
                func.apply(context, args);
                inThrottle = true;
                setTimeout(() => {
                    inThrottle = false;
                }, limit);
            }
        }
    }

    function s2() {
        console.log('s2');
    }
    throttle(s2, 1000)();


}
